package com.example.sunliangzhao;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatSpinner;

import android.graphics.Color;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.google.gson.Gson;
import com.qweather.plugin.view.HeContent;
import com.qweather.plugin.view.HorizonView;
import com.qweather.plugin.view.QWeatherConfig;
import com.qweather.plugin.view.SuspendView;
import com.qweather.sdk.bean.geo.GeoBean;
import com.qweather.sdk.bean.weather.WeatherDailyBean;
import com.qweather.sdk.bean.weather.WeatherNowBean;
import com.qweather.sdk.view.QWeather;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG="MainActivity";
    private String cityName="";
    private TextView tv_cityInfo;
    private TextView tv_weatherInfo;
    private TextView tv_fx_weatherInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /**
         * 初始化
         * @param key   用户的KEY
         * @param location  城市信息，若不传或为空则调用Android源生定位
         */
        QWeatherConfig.init("e6bba9ad640f487aaf99c364dac92f76","101200407");
        //横向布局
      /*  HorizonView horizonView = findViewById(R.id.horizon_view);
        //取消默认背景
        horizonView.setDefaultBack(true);
        //设置布局的背景圆角角度，颜色，边框宽度，边框颜色
        horizonView.setStroke(5, Color.BLUE,1,Color.BLUE);
        //添加地址文字描述，第一个参数为文字大小，单位：sp ，第二个参数为文字颜色，默认白色
        horizonView.addLocation(14, Color.WHITE);
        //添加预警图标，参数为图标大小，单位：dp
        horizonView.addAlarmIcon(14);
        //添加预警文字
        horizonView.addAlarmTxt(14);
        //添加温度描述
        horizonView.addTemp(14, Color.WHITE);
        //添加天气图标
        horizonView.addWeatherIcon(14);
        //添加天气描述
        horizonView.addCond(14, Color.WHITE);
        //添加风向图标
        horizonView.addWindIcon(14);
        //添加风力描述
        horizonView.addWind(14, Color.WHITE);
        //添加文字：AQI
        horizonView.addAqiText(14, Color.WHITE);
        //添加空气质量描述
        horizonView.addAqiQlty(14);
        //添加空气质量数值描述
        horizonView.addAqiNum(14);
        //添加降雨图标
        horizonView.addRainIcon(14);
        //添加降雨描述
        horizonView.addRainDetail(14, Color.WHITE);
        //设置控件的对齐方式，默认居中
        horizonView.setViewGravity(HeContent.GRAVITY_CENTER);
        //设置控件的内边距，默认为0
        horizonView.setViewPadding(10,10,10,10);
        //显示控件
        horizonView.show();*/



        tv_cityInfo=findViewById(R.id.tv_cityInfo);
        tv_weatherInfo=findViewById(R.id.tv_weatherInfo);
        tv_fx_weatherInfo=findViewById(R.id.tv_fx_weatherInfo);

        QWeather.getGeoCityLookup(MainActivity.this, "101200101", new QWeather.OnResultGeoListener() {
            @Override
            public void onError(Throwable throwable) {
                //城市信息不成功，则输出错误日志
                Log.d(TAG,"onError:"+throwable.getMessage());
            }

            @Override
            public void onSuccess(GeoBean geoBean) {
//                List<GeoBean.LocationBean> locationBean = geoBean.getLocationBean();
                Gson gson = new Gson();//初始化json对象
                Log.d(TAG,"onSuccess："+gson.toJson(geoBean));//用json的形式输出城市的信息
                List<GeoBean.LocationBean> locationBean = geoBean.getLocationBean();
                for(GeoBean.LocationBean location:locationBean){
                    String adm1 = location.getAdm1();//行政区
                    String adm2 = location.getAdm2();//上级城市
                    cityName= location.getName();//城市名
                    //String of_city=location.getCountry();//国家
                    tv_cityInfo.setText(adm2+"\t"+cityName);//设置城市名称
//                    Log.d(TAG, "onSuccess: " + cityName);
                }
            }
        });

        QWeather.getWeatherNow(MainActivity.this, "101200101",new QWeather.OnResultWeatherNowListener() {
            @Override
            public void onError(Throwable throwable) {
                Log.d(TAG,"onError:"+throwable.getMessage());
            }

            @Override
            public void onSuccess(WeatherNowBean weatherNowBean) {
                Gson gson = new Gson();//初始化json对象
                Log.d(TAG,"OnSuccess:"+gson.toJson(weatherNowBean));
                Log.d(TAG,"onSuccess:"+"天气:"+weatherNowBean.getNow().getText()+"    气温:"+weatherNowBean.getNow().getTemp());
                String text = weatherNowBean.getNow().getText();//当前天气
                String temp = weatherNowBean.getNow().getTemp()+"°C";//温度
                String icon = weatherNowBean.getNow().getIcon();//实况天气代码
                String windDir = weatherNowBean.getNow().getWindDir();//风向
                String wind360 = weatherNowBean.getNow().getWind360()+"°";//风向角度
                String humidity = weatherNowBean.getNow().getHumidity();//湿度
                String precip = weatherNowBean.getNow().getPrecip();//降水量
                weatherNowBean.getNow().getVis();//能见度
                tv_weatherInfo.setText("当前天气:"+text+"\t当前温度:"+temp+"\t"+"风向:"+windDir+"\t"+"风向角度:"+wind360+"\t"+"\n湿度:"+humidity+"\t"+"降水量:"+precip);

            }
        });

        QWeather.getWeather3D(MainActivity.this, "101200101", new QWeather.OnResultWeatherDailyListener() {
            @Override
            public void onError(Throwable throwable) {
                Log.d(TAG,"onError:"+throwable.getMessage());
            }

            @Override
            public void onSuccess(WeatherDailyBean weatherDailyBean) {
                List<WeatherDailyBean.DailyBean> daily = weatherDailyBean.getDaily();
                Gson gson=new Gson();
                Log.d(TAG,"onSuccess:"+gson.toJson(daily));
                for(WeatherDailyBean.DailyBean db:daily){
                    String fxDate = db.getFxDate();//预报日期
                    String textDay = db.getTextDay();//白天天气状况描述
                    String textNight = db.getTextNight();//夜间天气状态描述
                    String windDirDay = db.getWindDirDay();//白天风向
                    String windScaleDay = db.getWindScaleDay()+"级";//白天风力
                    String humidity = db.getHumidity();//相对湿度
                    String precip = db.getPrecip();//降水量
                    String vis = db.getVis();//能见度
                    tv_fx_weatherInfo.setText("日期:"+fxDate+"\t天气状况:"+textDay+"\t夜间状况:"+textNight
                            +"\n白天风向:"+windDirDay+"\t白天风力:"+windScaleDay+"\n湿度:"+humidity+"\t降水量:"+precip+"\t能见度:"+vis);

                }

            }
        });



    }



}